package com.stu.vertx.hello.verticle;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;

/**
 * 简单的路由使用
 * 
 * @author lenovo
 *
 */
public class SimpleRouter03 extends AbstractVerticle {
 
	@Override
	public void start() throws Exception {
 
		// 创建HttpServer
		HttpServer server = vertx.createHttpServer();
 
		// 创建路由对象
		Router router = Router.router(vertx);
 
		// 监听/index地址
		/*router.route("/index").handler(request -> {
			request.response().end("INDEX SUCCESS");
		});*/

		// 只允许post请求访问
		router.post("/post").handler(request -> {
			request.response().end("post");
		});

		// 只允许get请求访问
		router.route(HttpMethod.GET, "/method").handler(request -> {
			request.response().end("method");
		});
 
		// 把请求交给路由处理--------------------(1)
		server.requestHandler(router::accept);
		server.listen(8888);
	}
 
	public static void main(String[] args) {
		Vertx.vertx().deployVerticle(new SimpleRouter03());
	}
 
}